﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Collections;

namespace MemoryVerse
{
    [Serializable]
    public enum BibleBooks
    {
        Genesis,
        Exodus,
        Leviticus,
        Numbers,
        Deuteronomy,
        Joshua,
        Judges,
        Ruth,
        FirstSamuel,
        SecondSamuel,
        FirstKings,
        SecondKings,
        FirstChronicles,
        SecondChronicles,
        Ezra,
        Nehemiah,
        Esther,
        Job,
        Psalm,
        Proverbs,
        Ecclesiastes,
        SongOfSolomon,
        Isaiah,
        Jeremiah,
        Lamentations,
        Ezekiel,
        Daniel,
        Hosea,
        Joel,
        Amos,
        Obadiah,
        Jonah,
        Micah,
        Nahum,
        Habakkuk,
        Zephaniah,
        Haggai,
        Zechariah,
        Malachi,
        Matthew,
        Mark,
        Luke,
        John,
        Acts,
        Romans,
        FirstCorinthians,
        SecondCorinthians,
        Galatians,
        Ephesians,
        Philippians,
        Colossians,
        FirstThessalonians,
        SecondThessalonians,
        FirstTimothy,
        SecondTimothy,
        Titus,
        Philemon,
        Hebrews,
        James,
        FirstPeter,
        SecondPeter,
        FirstJohn,
        SecondJohn,
        ThirdJohn,
        Jude,
        Revelation
    }
    public static class LibraryManager
    {
        
        public static void SaveLibrary(VerseLibrary tosave)
        {
            XmlSerializer mySerializer = new
            XmlSerializer(tosave.GetType());
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter("VerseData.xml");
            mySerializer.Serialize(myWriter, tosave);
            myWriter.Close();
        }

        public static VerseLibrary LoadLibrary()
        {
            VerseLibrary myObject;
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.
            XmlSerializer mySerializer =
            new XmlSerializer(typeof(VerseLibrary));
            // To read the file, create a FileStream.
            FileStream myFileStream = new FileStream("VerseData.xml", FileMode.Open);
            // Call the Deserialize method and cast to the object type.
            myObject = (VerseLibrary)mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return myObject;
        }

 
    }
    [Serializable]
    public class VerseLibrary
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Verse> VerseList = new List<Verse>(); // a private list of verses

        public void Add(Verse v)
        {
            VerseList.Add(v);
        }
        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return (VerseList as IEnumerable).GetEnumerator();
        }

        #endregion
    }
    [Serializable]
    public class Verse
    {
        
        private BibleBooks _Book;
        public BibleBooks Book { 
            get { return _Book; }
            set { _Book = value; }
        }
        private int _Chapter;
        public int Chapter {
            get { return _Chapter; }
            set { _Chapter = value; }
        }
        private int _StartVerse;
        public int StartVerse {
            get { return _StartVerse; }
            set { _StartVerse = value; }
        }
        private int _EndVerse;
        public int EndVerse {
            get { return _EndVerse; }
            set { _EndVerse = value; }
        }
        private string _RawText;
        public string RawText {
            get { return _RawText; }
            set { _RawText = value; setPhrases();}
        }
        public Verse(){}

        public Verse(BibleBooks book, int chapter, int startverse, int endverse, string rawtext)
        {
            _Book = book;
            _Chapter = chapter;
            _StartVerse = startverse;
            _EndVerse = endverse;
            _RawText = rawtext;
            setPhrases();
        }

        /// <summary>
        /// A calculated method that returns the vers without the Seperators
        /// </summary>
        public string Text { get{return this.RawText.Replace("|","").Trim();} }
        [NonSerialized]
        public  List<QuizPhrase> Phrases;

        private void setPhrases()
        {
            this.Phrases = new List<QuizPhrase>(); 
            string[] phrases = this._RawText.Split('|');
            int count = 1;
            foreach (string phrase in phrases)
            {
                QuizPhrase QP = new QuizPhrase();
                QP.StartPosition = count;
                QP.Phrase = phrase + " " ;
                QP.Length = QP.Phrase.Length;
                Phrases.Add(QP);
            }
        }
        
    }

    public class QuizPhrase : IComparable<QuizPhrase>
    {
        public int StartPosition { get; set; }
        public int Length { get; set; }
        public string Phrase { get; set; }

        #region IComparable<QuizPhrase> Members

        public int CompareTo(QuizPhrase other)
        {
            if (this.StartPosition > other.StartPosition)
                return 1;
            else
                return -1;

        }

        #endregion
    }
}
